package com.zmzncs.lmtc.module.shop.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zmzncs.lmtc.common.pojo.PageInfo;
import com.zmzncs.lmtc.common.pojo.Rest;
import com.zmzncs.lmtc.common.util.JsonUtil;
import com.zmzncs.lmtc.common.util.RestUtil;
import com.zmzncs.lmtc.module.shop.pojo.shop_goods.*;
import com.zmzncs.lmtc.module.shop.pojo.shop_ticket.*;
import com.zmzncs.lmtc.module.shop.service.ShopTicketService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 商户-优惠券 前端控制器
 * </p>
 *
 * @author 高博文
 * @since 2021-05-20
 */
@Api(tags = "商户优惠券")
@RestController
@RequestMapping("/shop/shop-ticket")
public class ShopTicketController {

    @Autowired
    ShopTicketService shopTicketService;

    @ApiOperation(value = "添加优惠券")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
    })
    @PostMapping("add")
    public Rest add(@Valid ShopTicketAddForm form){
        form.validate();
        shopTicketService.add(form);

        return RestUtil.success();
    }

    @ApiOperation(value = "条件分页查询优惠券信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
    })
    @PostMapping("getTicketPage")
    public Rest<PageInfo<ShopTicketTableVO>> getTicketPage(@Valid ShopTicketSelectForm form){
        IPage<ShopTicketDTO> ticketPage = shopTicketService.getTicketPage(form);
        PageInfo<ShopTicketTableVO> pageInfo = JsonUtil.pageTransform(ticketPage, ShopTicketTableVO.class);
        return RestUtil.success(pageInfo);
    }

    @ApiOperation(value = "券类管理—删除")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
            @ApiImplicitParam(paramType="query", name = "ticketId", value = "优惠券ID", required = true),
    })
    @PostMapping("delete")
    public Rest delete(@NotNull Long ticketId){

        ShopTicket ticket = new ShopTicket();
        ticket.setId(ticketId);
        ticket.setIsDeleted(1);
        shopTicketService.updateById(ticket);

        return RestUtil.success();
    }

    @ApiOperation(value = "券类管理—修改展示状态")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
            @ApiImplicitParam(paramType="query", name = "ticketId", value = "优惠券ID", required = true),
            @ApiImplicitParam(paramType="query", name = "status", value = "状态（1：展示，0：不展示）", required = true),
    })
    @PostMapping("updateStatus")
    public Rest updateStatus(@NotNull Long ticketId, @NotNull Integer status){

        ShopTicket ticket = new ShopTicket();
        ticket.setId(ticketId);
        ticket.setStatus(status);
        shopTicketService.updateById(ticket);

        return RestUtil.success();
    }

    @ApiOperation(value = "查询商户所有优惠券信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
            @ApiImplicitParam(paramType="query", name = "shopId", value = "商户ID", required = true),
    })
    @PostMapping("getTicketByShopId")
    public Rest<List<ShopTicketDTO>> getTicketByShopId(@NotNull Long shopId){
        List<ShopTicketDTO> shopTicketDTOS =  shopTicketService.getTicketByShopId(shopId);
        return RestUtil.success(shopTicketDTOS);
    }

}

